package com.ChenYaRou.www.utils;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.List;

import static com.ChenYaRou.www.constant.JsonBeanConstant.STRING_NULL;

/**
 * @ClassName JsonBean
 * @Description TODO
 * @Author 86183
 * @Date 2022/4/30 23:32
 * @Version 1.0
 **/
public  class JsonBean {
    private static HashMap<String,Object> jsonBean= new HashMap<>();

    public  JsonBean() {}

    /**
     * 基层生成json语句
     * @param code
     * @param state
     * @param object
     * @return
     */
    public static String generateJson(Integer code,String state,Object object)  {
        jsonBean.put("code",code);
        jsonBean.put("state",state);
        if(object!=null){
            jsonBean.put("data", JSON.toJSONString(object));
        }else {
            jsonBean.put("data", STRING_NULL);
        }
        return JSON.toJSONString(jsonBean);

    }

    /**
     * 对单集合操作制作json 同时集合参数null为生成失败json语句且不带参数
     * @param list
     * @param <K>
     * @return
     */
    public static  <K> String operateList(String state,List<K> list)  {

        if(list!=null && !list.isEmpty()){
            return generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,list);
        }
        String json=null;
        if(state!=null){
                json = generateJson(JsonBeanConstant.NOT_GET,state,null);
        }
        return json;
    }
}
